package org.one.stone.soup.grfxML.plugin.system;

import org.one.stone.soup.grfxML.DataPoint;
import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class SystemMonitor extends SimplePlugin{

	DataState on = new DataState();
	DataString memoryUnit = new DataString();
	DataString loadUnit = new DataString();
	DataPoint memoryBar = new DataPoint();
	DataPoint behaviourBar = new DataPoint();
	DataPoint actionBar = new DataPoint();
	DataPoint translationBar = new DataPoint();
	DataPoint nodeBar = new DataPoint();
	DataPoint geometryBar = new DataPoint();

	DataString memoryLabel = new DataString();
	DataString behaviourLabel = new DataString();
	DataString actionLabel = new DataString();
	DataString translationLabel = new DataString();
	DataString nodeLabel = new DataString();
	DataString geometryLabel = new DataString();
/**
 * FileBrowser constructor comment.
 */
public SystemMonitor(GrfxMLEngine engine) {
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize() {}
public void process()
{
  try{

	  if(on==null)
	  	return;

//	  if(on.getValue()==false)
//	  	return;

	long max = 1;

	if( getEngine().getAPI().getProfile().getNODE_LOAD().getValue() > max )
		max = getEngine().getAPI().getProfile().getNODE_LOAD().getValue();
	if( getEngine().getAPI().getProfile().getTRANSLATION_LOAD().getValue() > max )
		max = getEngine().getAPI().getProfile().getTRANSLATION_LOAD().getValue();
	if( getEngine().getAPI().getProfile().getBEHAVIOUR_LOAD().getValue() > max )
		max = getEngine().getAPI().getProfile().getBEHAVIOUR_LOAD().getValue();
	if( getEngine().getAPI().getProfile().getACTION_LOAD().getValue() > max )
		max = getEngine().getAPI().getProfile().getACTION_LOAD().getValue();
	if( getEngine().getAPI().getProfile().getGEOMETRY_LOAD().getValue() > max )
		max = getEngine().getAPI().getProfile().getGEOMETRY_LOAD().getValue();

	int factor = 1;

	while(max/factor>100)
	{
		factor*=2;
	}

	String unit = "x"+factor;

	loadUnit.setValue(unit,this);
	nodeBar.setValueX( getEngine().getAPI().getProfile().getNODE_LOAD().getValue()/factor ,this);
	translationBar.setValueX( getEngine().getAPI().getProfile().getTRANSLATION_LOAD().getValue()/factor ,this);
	behaviourBar.setValueX( getEngine().getAPI().getProfile().getBEHAVIOUR_LOAD().getValue()/factor ,this);
	actionBar.setValueX( getEngine().getAPI().getProfile().getACTION_LOAD().getValue()/factor ,this);
	geometryBar.setValueX( getEngine().getAPI().getProfile().getGEOMETRY_LOAD().getValue()/factor ,this);

	nodeLabel.setValue("Nodes processed: "+getEngine().getAPI().getProfile().getNODE_LOAD().getValue(),this);
	translationLabel.setValue("Translations Performed "+getEngine().getAPI().getProfile().getTRANSLATION_LOAD().getValue(),this);
	behaviourLabel.setValue("Behaviours processed: "+getEngine().getAPI().getProfile().getBEHAVIOUR_LOAD().getValue(),this);
	actionLabel.setValue("Actions processed: "+getEngine().getAPI().getProfile().getACTION_LOAD().getValue(),this);
	geometryLabel.setValue("Geometrys rendered: "+getEngine().getAPI().getProfile().getGEOMETRY_LOAD().getValue(),this);

	memoryUnit.setValue("%",this);
	memoryBar.setValueX( (int)(Runtime.getRuntime().freeMemory()*100/Runtime.getRuntime().totalMemory()) ,this);
	memoryLabel.setValue("Memory left: "+memoryBar.getValueX()+" %",this);

  }
  catch(LockException le){}//dummy catch
}
public void register(DataLinker store)
{

	on = 				grfxMLCaster.cast(on,getArg(on,0,store));
	memoryUnit = 		grfxMLCaster.cast(memoryUnit,getArg(memoryUnit,1,store));
	loadUnit = 			grfxMLCaster.cast(loadUnit,getArg(loadUnit,2,store));
	memoryBar = 		grfxMLCaster.cast(memoryBar,getArg(memoryBar,3,store));
	nodeBar = 			grfxMLCaster.cast(nodeBar,getArg(nodeBar,4,store));
	translationBar = 	grfxMLCaster.cast(translationBar,getArg(translationBar,5,store));
	behaviourBar = 		grfxMLCaster.cast(behaviourBar,getArg(behaviourBar,6,store));
	actionBar = 		grfxMLCaster.cast(actionBar,getArg(actionBar,7,store));
	geometryBar = 		grfxMLCaster.cast(geometryBar,getArg(geometryBar,8,store));

	memoryLabel = 		grfxMLCaster.cast(memoryLabel,getArg(memoryLabel,9,store));
	nodeLabel = 		grfxMLCaster.cast(nodeLabel,getArg(nodeLabel,10,store));
	translationLabel = 	grfxMLCaster.cast(translationLabel,getArg(translationLabel,11,store));
	behaviourLabel = 	grfxMLCaster.cast(behaviourLabel,getArg(behaviourLabel,12,store));
	actionLabel =		grfxMLCaster.cast(actionLabel,getArg(actionLabel,13,store));
	geometryLabel =		grfxMLCaster.cast(geometryLabel,getArg(geometryLabel,14,store));
}
public void replace(Field oldObj, Field newObj)
{
	on =				grfxMLCaster.replace(on,oldObj,newObj);
	memoryUnit =		grfxMLCaster.replace(memoryUnit,oldObj,newObj);
	loadUnit =			grfxMLCaster.replace(loadUnit,oldObj,newObj);
	memoryBar =			grfxMLCaster.replace(memoryBar,oldObj,newObj);
	nodeBar =			grfxMLCaster.replace(nodeBar,oldObj,newObj);
	translationBar =	grfxMLCaster.replace(translationBar,oldObj,newObj);
	behaviourBar =		grfxMLCaster.replace(behaviourBar,oldObj,newObj);
	actionBar =			grfxMLCaster.replace(actionBar,oldObj,newObj);
	geometryBar =		grfxMLCaster.replace(geometryBar,oldObj,newObj);

	memoryLabel = 		grfxMLCaster.replace(memoryLabel,oldObj,newObj);
	nodeLabel = 		grfxMLCaster.replace(nodeLabel,oldObj,newObj);
	translationLabel = 	grfxMLCaster.replace(translationLabel,oldObj,newObj);
	behaviourLabel = 	grfxMLCaster.replace(behaviourLabel,oldObj,newObj);
	actionLabel =		grfxMLCaster.replace(actionLabel,oldObj,newObj);
	geometryLabel =		grfxMLCaster.replace(geometryLabel,oldObj,newObj);

}
/**
 * stop method comment.
 */
public void stop() {}
}
